package com.yilu.driver.ui.activity;

import android.Manifest;
import android.content.Intent;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

import com.is.common.permissions.PermissionCallback;
import com.is.common.utils.ImageLoaderUtils;
import com.is.common.utils.SPUtils;
import com.is.ui.superdialog.SuperDialog;
import com.is.ui.widget.IconButton;
import com.yilu.driver.DriverConstant;
import com.yilu.driver.R;
import com.yilu.driver.base.BaseActivity;
import com.yilu.driver.entity.DeleteTraineeResponse;
import com.yilu.driver.entity.TraineeDropResponse;
import com.yilu.driver.entity.TraineeInfoResponse;
import com.yilu.driver.mvp.contract.TraineeInfoContract;
import com.yilu.driver.mvp.model.TraineeInfoModel;
import com.yilu.driver.mvp.presenter.TraineeInfoPresenter;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;


/**
 * 学员信息详情
 *
 * @author Administrator
 * @date 2017/4/11 0011
 */

public class TraineeInfoActivity extends BaseActivity implements TraineeInfoContract.View {

    @Bind(R.id.ib_back)
    ImageButton ibBack;
    @Bind(R.id.ib_exit)
    ImageButton ibExit;
    @Bind(R.id.txt_title)
    TextView txtTitle;
    @Bind(R.id.ib_search)
    ImageButton ibSearch;
    @Bind(R.id.ib_menu)
    ImageButton ibMenu;
    @Bind(R.id.img_head)
    ImageView imgHead;
    @Bind(R.id.txt_update)
    TextView txtUpdate;
    @Bind(R.id.txt_drop_out)
    TextView txtDropOut;
    @Bind(R.id.txt_delete)
    TextView txtDelete;
    @Bind(R.id.txt_archivesNumber)
    TextView txtArchivesNumber;
    @Bind(R.id.txt_idNumber)
    TextView txtIdNumber;
    @Bind(R.id.txt_birthday)
    TextView txtBirthday;
    @Bind(R.id.txt_sex)
    TextView txtSex;
    @Bind(R.id.txt_periods)
    TextView txtPeriods;
    @Bind(R.id.txt_enrolTime)
    TextView txtEnrolTime;
    @Bind(R.id.txt_mobile)
    TextView txtMobile;
    @Bind(R.id.txt_applyType)
    TextView txtApplyType;
    @Bind(R.id.txt_haveCar)
    TextView txtHaveCar;
    @Bind(R.id.txt_entryWay)
    TextView txtEntryWay;
    @Bind(R.id.txt_fieldCoach)
    TextView txtFieldCoach;
    @Bind(R.id.txt_roadCoach)
    TextView txtRoadCoach;
    @Bind(R.id.txt_physicalExamination)
    TextView txtPhysicalExamination;
    @Bind(R.id.txt_contactAddress)
    TextView txtContactAddress;
    @Bind(R.id.txt_company)
    TextView txtCompany;
    @Bind(R.id.txt_position)
    TextView txtPosition;
    @Bind(R.id.txt_homeAddress)
    TextView txtHomeAddress;
    @Bind(R.id.txt_serviceType)
    TextView txtServiceType;
    @Bind(R.id.txt_deptName)
    TextView txtDeptName;
    @Bind(R.id.txt_operator)
    TextView txtOperator;
    @Bind(R.id.txt_cost)
    TextView txtCost;
    @Bind(R.id.txt_arrearager)
    TextView txtArrearager;
    @Bind(R.id.btn_call)
    IconButton btnCall;
    @Bind(R.id.txt_name)
    TextView txtName;
    private TraineeInfoResponse.DataBean traineeInfo;
    private int id;
    private Integer operator;//操作人
    /**
     * 数据请求相关
     */
    private TraineeInfoModel model;
    private TraineeInfoPresenter presenter;

    @Override
    public void initPresenter() {
        model = new TraineeInfoModel();
        presenter = new TraineeInfoPresenter(mContext);
        presenter.setVM(this, model);
    }

    /**
     * 获取Bundle 的数据
     *
     * @param extras
     */
    @Override
    protected void getBundleExtras(Bundle extras) {
        id = extras.getInt("id");
    }

    @Override
    protected int getContentViewLayoutID() {
        return R.layout.activity_trainee_info;
    }

    @Override
    protected void initViewsAndEvents() {
        txtTitle.setText("学员信息查看");
        getTraineeInfo(id);//获取详情
        operator = (Integer) SPUtils.get(mContext, "id", 0);
    }

    @OnClick({R.id.ib_back, R.id.txt_update, R.id.txt_drop_out, R.id.txt_delete, R.id.btn_call})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.ib_back:
                finish();
                break;
            case R.id.txt_update:
                break;
            case R.id.txt_drop_out:
                new SuperDialog.Builder(this).setTitle("退学").setMessage("确定要退学这个学员吗？")
                        .setBackgroundColor(Color.WHITE)
                        .setNegativeButton("取消", null)
                        .setPositiveButton("确定", new SuperDialog.OnClickPositiveListener() {
                            @Override
                            public void onClick(View v) {
                                dropOutTrainee(traineeInfo.getId(), traineeInfo.getName(), operator);
                            }
                        }).build();
                break;
            case R.id.txt_delete:
                new SuperDialog.Builder(this).setTitle("删除").setMessage("确定删除这个学员吗？")
                        .setBackgroundColor(Color.WHITE)
                        .setNegativeButton("取消", null)
                        .setPositiveButton("确定", new SuperDialog.OnClickPositiveListener() {
                            @Override
                            public void onClick(View v) {
                                deleteTrainee(traineeInfo.getId(), traineeInfo.getName(), operator);
                            }
                        }).build();
                break;
            case R.id.btn_call:
                DialTask();
                break;
        }
    }
    /**
     * 拨打电话
     */
    /**
     * 拨打电话
     */
    private void onCall() {
        try {
            Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + traineeInfo.getMobile()));
            startActivity(intent);
        } catch (SecurityException s) {
            s.printStackTrace();
        }

    }

    /**
     * 权限申请 拨打电话
     */
    public void DialTask() {
        String[] perms = {Manifest.permission.CALL_PHONE};
        performCodeWithPermission(getString(R.string.rationale_phone), DriverConstant.REQUEST_CALL_PHONE_PERMISSION, perms, new PermissionCallback() {
            @Override
            public void hasPermission(List<String> allPerms) {
                onCall();
            }

            @Override
            public void noPermission(List<String> deniedPerms, List<String> grantedPerms, Boolean hasPermanentlyDenied) {
                if (hasPermanentlyDenied) {
                    alertAppSetPermission(getString(R.string.rationale_ask_again), DriverConstant.REQUEST_CALL_PHONE_PERMISSION);
                }
            }
        });
    }

    /**
     * 获取学员详情
     */
    public void getTraineeInfo(Integer id) {
        JSONObject json = new JSONObject();
        try {
            json.put("id", id);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        presenter.getTraineeInfo(json);
    }

    /**
     * 删除学员
     *
     * @param
     */
    public void deleteTrainee(Integer id, String name, Integer operator) {
        JSONObject json = new JSONObject();
        try {
            json.put("id", id);
            json.put("name", name);
            json.put("operator", operator);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        presenter.deleteTrainee(json);
    }

    /**
     * 学员退学
     *
     * @param id
     * @param name
     */
    public void dropOutTrainee(Integer id, String name, Integer operator) {
        JSONObject json = new JSONObject();
        try {
            json.put("id", id);
            json.put("name", name);
            json.put("operator", operator);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        presenter.dropOutTrainee(json);
    }

    /**
     * 查看详情 更新UI
     *
     * @param traineeInfoResponse
     */
    @Override
    public void returnTraineeInfo(TraineeInfoResponse traineeInfoResponse) {
        traineeInfo = traineeInfoResponse.getData();
        txtName.setText(traineeInfo.getName());
        ImageLoaderUtils.displayRound(mContext, imgHead, traineeInfo.getHeader());
        txtArchivesNumber.setText(traineeInfo.getArchivesNumber());
        txtIdNumber.setText(traineeInfo.getIdNumber());
        txtBirthday.setText(traineeInfo.getBirthday());
        if (traineeInfo.getSex() == 1) {
            txtSex.setText("男");
        } else {
            txtSex.setText("女");
        }
        txtPeriods.setText(traineeInfo.getPeriods());
        txtEnrolTime.setText(traineeInfo.getEnrolTime());
        txtMobile.setText(traineeInfo.getMobile());
        txtApplyType.setText(traineeInfo.getApplyType());
        if (traineeInfo.getHaveCar() == 1) {
            txtHaveCar.setText("是");
        } else {
            txtHaveCar.setText("否");
        }
        txtEntryWay.setText(traineeInfo.getEntryWay());
        txtFieldCoach.setText(traineeInfo.getFieldCoach());
        txtCost.setText("￥" + traineeInfo.getCost() + "元");
        txtArrearager.setText("￥" + traineeInfo.getArrearager() + "元");
        txtRoadCoach.setText(traineeInfo.getRoadCoach());
        if (traineeInfo.getPhysicalExamination() == 1) {
            txtPhysicalExamination.setText("是");
        } else {
            txtPhysicalExamination.setText("否");
        }
        txtContactAddress.setText(traineeInfo.getContactAddress());
        txtCompany.setText(traineeInfo.getCompany());
        txtPosition.setText(traineeInfo.getPosition());
        txtHomeAddress.setText(traineeInfo.getHomeAddress());
        txtServiceType.setText(traineeInfo.getServiceType());
        txtOperator.setText(traineeInfo.getOperator());
        txtDeptName.setText(traineeInfo.getDeptName());
    }

    @Override
    public void returnDropOutTrainee(TraineeDropResponse traineeDropResponse) {
        showToast(traineeDropResponse.getData());
        this.finish();
    }

    @Override
    public void returnDeleteTrainee(DeleteTraineeResponse deleteTraineeResponse) {
        showToast("删除成功");
        this.finish();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }
}
